package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.ProOrder;
import com.ruoyi.common.core.domain.entity.ProOrderDetail;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.ITblProOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 加工订单 业务层处理
 *
 * @author ruoyi
 */
@Service
public class TblProOrderServiceImpl implements ITblProOrderService {
    private static final Logger log = LoggerFactory.getLogger(TblProOrderServiceImpl.class);

    @Autowired
    private TblProOrderMapper tblProOrderMapper;

    /**
     * 根据条件分页查询订单列表
     *
     * @param order 用户信息
     */
    @Override
    public List<ProOrder> selectProOrderList(ProOrder order) {
        return tblProOrderMapper.selectProOrderList(order);
    }

    /**
     * 根据订单编号获取该加工订单
     *
     * @param orderCode 订单编号
     * @return
     */
    @Override
    public ProOrder selectProOrderByCode(String orderCode) {
        return tblProOrderMapper.selectProOrderByCode(orderCode);
    }

    /**
     * 导入加工订单
     *
     * @param proOrder           订单数据列表
     * @param proOrderDetailList 订单详情列表数据列表
     * @param operName           操作用户
     * @return
     */
    @Transactional
    @Override
    public String importProOrder(ProOrder proOrder, List<ProOrderDetail> proOrderDetailList, String operName) {
        if (StringUtils.isNull(proOrder) || StringUtils.isNull(proOrderDetailList) || proOrderDetailList.size() == 0) {
            throw new ServiceException("导入订单数据不能为空！");
        }

        proOrder.setCreateBy(operName);
        try {
            tblProOrderMapper.insertProOrder(proOrder);
        } catch (Exception e) {
            return "加工订单导入失败：" + e.getMessage();
        }

        for (ProOrderDetail detail : proOrderDetailList) {
            detail.setOrderCode(proOrder.getOrderCode());
            detail.setCreateBy(operName);
            if (StringUtils.isEmpty(detail.getChildTaskCode())) {
                detail.setChildTaskCode(detail.getPartNumber() + "-1");
            }
        }

        try {
            tblProOrderMapper.insertAllProOrderDetail(proOrderDetailList);
        } catch (Exception e) {
            return "加工订单任务导入失败：" + e.getMessage();
        }


        return "加工订单数据全部导入成功";
    }

    /**
     * 根据条件分页查询订单详情任务列表
     *
     * @param proOrderDetail 用户信息
     */
    @Override
    public List<ProOrderDetail> selectProOrderDetailList(ProOrderDetail proOrderDetail) {
        return tblProOrderMapper.selectProOrderDetailList(proOrderDetail);
    }

    /**
     * 删除加工订单以及订单详情数据
     *
     * @param orderCodes 需要删除的订单code
     * @return
     */
    @Transactional
    @Override
    public int deleteOrderByCodes(String[] orderCodes) {
        return tblProOrderMapper.deleteOrderByCodes(orderCodes) + tblProOrderMapper.deleteOrderDetailByCodes(orderCodes);
    }


    /**
     * 删除订单详情任务
     *
     * @param orderIds 需要删除的订单code
     * @return
     */
    @Override
    public int deleteOrderDetailByIds(String[] orderIds) {
        return tblProOrderMapper.deleteOrderDetailByIds(orderIds);
    }

    @Override
    public int insertProOrder(ProOrder proOrder) {
        return tblProOrderMapper.insertProOrder(proOrder);
    }

    @Override
    public int updateProOrder(ProOrder proOrder) {
        return tblProOrderMapper.updateProOrder(proOrder);
    }

    @Override
    public List<String> selectAllOrderCode(String orderCode) {
        return tblProOrderMapper.selectAllOrderCode(orderCode);
    }

    @Override
    public int insertProOrderDetail(ProOrderDetail proOrderDetail) {
        proOrderDetail.setChildTaskCode(proOrderDetail.getPartNumber() + "-1");
        return tblProOrderMapper.insertProOrderDetail(proOrderDetail);
    }

    @Override
    public int updateProOrderDetail(ProOrderDetail proOrderDetail) {
        return tblProOrderMapper.updateProOrderDetail(proOrderDetail);
    }

    @Override
    public int deleteOrderDetailById(Long orderId) {
        return tblProOrderMapper.deleteOrderDetailById(orderId);
    }

    @Transactional
    @Override
    public int splitOrderTask(Long orderId, int taskNum) {
        ProOrderDetail detail = tblProOrderMapper.selectOrderDetailById(orderId);
        ProOrderDetail split = detail;
        tblProOrderMapper.updateOrderDetailNum(detail.getOrderId(), detail.getNum() - taskNum);
        split.setNum(taskNum);
        String sub = detail.getChildTaskCode().substring(0, detail.getChildTaskCode().lastIndexOf("-") + 1);
        List<String> taskCodeLists = tblProOrderMapper.selectAllOrderChildTaskCode(sub);
        String childTaskCode = taskCodeLists.get(taskCodeLists.size() - 1);
        Integer number =  Integer.parseInt(childTaskCode.substring(childTaskCode.lastIndexOf("-") + 1));
        number++;
        split.setChildTaskCode(sub + number);
        return tblProOrderMapper.insertProOrderDetail(split);
    }

}
